﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamePlugin.Logics.Core;
using WindowsInput;

namespace GamePlugin.Logics.Game.Actions
{
    public class DActionAuctionStart : MachineState
    {
        public DateTime ReceiveItemDate = DateTime.Now;

        public bool IsGrabDataMode
        {
            get;
            set;
        }

        public DActionAuctionStart()
        {
            IsGrabDataMode = true;
        }

        public override void Initialize()
        {

            this.AddState("DetermineNextStep");
            this["DetermineNextStep"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.DetermineNextStep(o, e));

            this.DefaultState = "DetermineNextStep";
        }

        public void DetermineNextStep(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            if (input.InputDeviceState.IsHardwareKeyDown(WindowsInput.Native.VirtualKeyCode.F1))
            {
                this.IsGrabDataMode = false;
            }

            if (this.IsGrabDataMode)
                this.Owner.ChangeToState(typeof(DActionAuctionGrabData).Name);
            else
            {
                if (DateTime.Now.Subtract(this.ReceiveItemDate).TotalMinutes > 10)
                {
                    this.Owner.ChangeToState(typeof(DActionAuctionReceiveItems).Name);
                }
                else
                {
                    int index = -1;
                    for (int i = 0; i < Workspace.Instance.AuctionItemList.Children.Count; i++)
                        if (Workspace.Instance.AuctionItemList.Children[i].IsExactDeadline == false &&
                            Workspace.Instance.AuctionItemList.Children[i].ItemCounldBeFound == true)
                        {
                            index = -1;
                            Helper.GetMainWindow().AuctionControl.AuctionItemListBox.SelectedIndex = i;
                            this.Owner.ChangeToState(typeof(DActionAuctionCollectDeadline).Name);
                            break;
                        }

                    if (index == -1)
                        this.Owner.ChangeToState(typeof(DActionAuctionTryAuctionItem).Name);
                }
            }
        }
    }
}
